﻿package cn.poco.pepsi;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;
import cn.poco.pepsi.R.drawable;
import cn.poco.pepsi.activity.discut.DiscutActivity;
import cn.poco.pepsi.activity.ipepsi.IpepsiMainActivity;
import cn.poco.pepsi.activity.ipepsi.LoginActivity;
import cn.poco.pepsi.activity.member.MemberActivity;
import cn.poco.pepsi.activity.near.NearActivity;
import cn.poco.pepsi.activity.onhere.OnHereMainActivity;
import cn.poco.pepsi.activity.search.SearchMainActivity;
import cn.poco.pepsi.bean.RemindSum;
import cn.poco.pepsi.service.impl.RemindImpl;
import cn.poco.pepsi.util.Cons;


public class TabActivity extends android.app.TabActivity {
	public static final String TAG = "TabActivity ";
	public static TabHost tabHost;
	TabWidget tabWidget;  
	int tabChange;
	private RemindSum reSum;
	TextView tv1;
	TextView tv2;
	TextView tvSum;
	TextView tv3;
	TextView tv4;
	TextView tv5;
	String pocoid;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.init_tab);
		
		
		tabHost = getTabHost();
		tabWidget = getTabWidget();
		reSum = (RemindSum) getIntent().getSerializableExtra(Cons.INIT_REMINDSUM);
		String cityCode = getIntent().getStringExtra(Cons.CITY_CODE);
		String cityName = getIntent().getStringExtra(Cons.CITY_NAME);
		tabChange = getIntent().getIntExtra(Cons.TabChange, 0);
		

		SharedPreferences sf = TabActivity.this.getSharedPreferences(Cons.SFPocoUser, 0);
		final String pocoid = sf.getString(Cons.SFPocoId, "0");

		Intent intent = new Intent(this, SearchMainActivity.class);
		Bundle bundle = new Bundle();
		bundle.putString(Cons.CITY_CODE, cityCode);
		bundle.putString(Cons.CITY_NAME, cityName);
		intent.putExtras(bundle);
		
		
		
        Intent intentRemind = new Intent(this, NearActivity.class);
		intentRemind.putExtra(Cons.INIT_REMINDSUM, reSum);
		
		Intent intentForTakePhoto = new Intent(this, DiscutActivity.class);
		intentForTakePhoto.putExtra(Cons.MAINVIEW_TO_TAKE_PHOTO, 1);
		
		Intent intentforMember = new Intent(this, OnHereMainActivity.class);
		intentforMember.putExtra(Cons.MemeberUid, Integer.parseInt(pocoid));
		intentforMember.putExtra(Cons.MemeOnType, 0);
		
		Intent ipepsi=new Intent(this,IpepsiMainActivity.class);

		setIndicator(drawable.activity_icon, "搜索", intent);
		setIndicatorSum(drawable.activity_icon, "附近", intentRemind, reSum);
		setIndicator(drawable.activity_icon, "优惠", intentForTakePhoto);
		setIndicator(drawable.activity_icon, "签到", intentforMember);
		setIndicator(drawable.activity_icon, "i百事", ipepsi);

		ImageView iv1 = null, iv2 = null, iv3 = null, iv4 = null, iv5 = null;
		for (int i = 0; i < tabWidget.getChildCount(); i++) {
			if (i == 0) {
				tv1 = (TextView) tabWidget.getChildAt(i).findViewById(R.id.main_activity_tab_text);
				iv1 = (ImageView) tabWidget.getChildAt(i).findViewById(R.id.main_activity_tab_image);
			} else if (i == 1) {
				tv2 = (TextView) tabWidget.getChildAt(i).findViewById(R.id.main_activity_tab_text);
				iv2 = (ImageView) tabWidget.getChildAt(i).findViewById(R.id.main_activity_tab_image);
			} else if (i == 2) {
				tv3 = (TextView) tabWidget.getChildAt(i).findViewById(R.id.main_activity_tab_text);
				iv3 = (ImageView) tabWidget.getChildAt(i).findViewById(R.id.main_activity_tab_image);
			} else if (i == 3) {
				tv4 = (TextView) tabWidget.getChildAt(i).findViewById(R.id.main_activity_tab_text);
				iv4 = (ImageView) tabWidget.getChildAt(i).findViewById(R.id.main_activity_tab_image);
			} else if (i == 4) {
				tv5 = (TextView) tabWidget.getChildAt(i).findViewById(R.id.main_activity_tab_text);
				iv5 = (ImageView) tabWidget.getChildAt(i).findViewById(R.id.main_activity_tab_image);
			}
		}

		// 第一个选项卡初始化选中颜色
		TextView tv1Out = (TextView) tabWidget.getChildAt(0).findViewById(R.id.main_activity_tab_text);
		// ImageView iv1Out = (ImageView)
		// tabWidget.getChildAt(0).findViewById(R.id.main_activity_tab_image);
		tv1Out.setTextColor(Color.BLACK);

		// 图片不改变
		iv1.setImageResource(R.drawable.main_tab_1);
		
		iv2.setImageResource(R.drawable.main_tab_2);
		
		iv3.setImageResource(R.drawable.main_tab_3);
		
		iv4.setImageResource(R.drawable.main_tab_4);
		
		iv5.setImageResource(R.drawable.main_tab_5);
		

		// 动态切换图片及文字
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				// String[] values={"首页","提醒","拍照","主页","更多"};
				// int l=0;

				if (tabId.equals("搜索")) {
					tv1.setTextColor(Color.BLACK);
				} else {
					tv1.setTextColor(Color.WHITE);
				}
				if (tabId.equals("附近")) {
					tvSum.setVisibility(View.GONE);
					tv2.setTextColor(Color.BLACK);
				} else {
					tv2.setTextColor(Color.WHITE);
				}
				if (tabId.equals("优惠")) {
					tv3.setTextColor(Color.BLACK);
				} else {
					tv3.setTextColor(Color.WHITE);
				}
				if (tabId.equals("签到")) {
					tv4.setTextColor(Color.BLACK);
				} else {
					tv4.setTextColor(Color.WHITE);
				}
				if (tabId.equals("i百事")) {
					tv5.setTextColor(Color.BLACK);
				} else {
					tv5.setTextColor(Color.WHITE);
				}
			}
		});
		IntentFilter filter = new IntentFilter();
		filter.addAction("cn.poco.food.loginAction");
		registerReceiver(mReceiver, filter);
	}

	protected BroadcastReceiver mReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			pocoid = intent.getStringExtra(Cons.LOGIN_RECEIVE_VALUE);
			if (!pocoid.equals("0")) {
				tvSum.setText("");
				new Thread(new Runnable() {
					@Override
					public void run() {
						try {
							reSum = new RemindImpl().getRemaindSum(pocoid);
							handler.sendEmptyMessage(1);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}).start();
			}
		}
	};

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 1) {
				tvSum.setVisibility(View.VISIBLE);
				tvSum.setText(String.valueOf(Integer.parseInt(reSum.getFollowSum())
						+ Integer.parseInt(reSum.getSystemSum()) + Integer.parseInt(reSum.getZanSum())));
			}
		};
	};

	private void setIndicator(int icon, String title, Intent view) {
		View localView = LayoutInflater.from(tabHost.getContext()).inflate(R.layout.int_tab_layout, null);
		((ImageView) localView.findViewById(R.id.main_activity_tab_image)).setImageResource(icon);
		((TextView) localView.findViewById(R.id.main_activity_tab_text)).setText(title);
		TabHost.TabSpec localTabSpec = tabHost.newTabSpec(title).setIndicator(localView).setContent(view);
		tabHost.addTab(localTabSpec);
	}

	private void setIndicatorSum(int icon, String title, Intent view, RemindSum reSum) {
		View localView = LayoutInflater.from(tabHost.getContext()).inflate(R.layout.int_tab_remind_layout, null);
		((ImageView) localView.findViewById(R.id.main_activity_tab_image)).setImageResource(icon);
		((TextView) localView.findViewById(R.id.main_activity_tab_text)).setText(title);
		tvSum = (TextView) localView.findViewById(R.id.main_activity_tab_remind_count);
		if (reSum != null) {
			tvSum.setText(String.valueOf(Integer.parseInt(reSum.getFollowSum())
					+ Integer.parseInt(reSum.getSystemSum()) + Integer.parseInt(reSum.getZanSum())));
		} else {
			tvSum.setVisibility(View.GONE);
		}
		TabHost.TabSpec localTabSpec = tabHost.newTabSpec(title).setIndicator(localView).setContent(view);
		tabHost.addTab(localTabSpec);
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// MenuInflater menuInflater = getMenuInflater();
		// menuInflater.inflate(R.xml.main_menu, menu);
		menu.add(Menu.NONE, Menu.FIRST + 2, 1, "设置").setIcon(android.R.drawable.ic_menu_set_as);
		menu.add(Menu.NONE, Menu.FIRST + 3, 2, "登录").setIcon(android.R.drawable.ic_menu_slideshow);
		menu.add(Menu.NONE, Menu.FIRST + 4, 3, "注销").setIcon(android.R.drawable.ic_menu_add);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		return super.onPrepareOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case Menu.FIRST + 3:
			//TabTest.class
			Intent intent = new Intent(TabActivity.this, LoginActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}
